package com.skt.common_hr.overtime.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.overtime.model.vo.DeptOtVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class DeptOtProxy extends CommonProxy
	{
		public static const NAME:String = "DeptOtProxy";
		public static const LOAD_DEPT_OT_COMPLETE:String = "loadDeptOtComplete";
		public static const LOAD_DEPT_OT_FAILED:String = "loadDeptOtFailed";
		public static const SAVE_DEPT_OT_COMPLETE:String = "saveDeptOtComplete";
		public static const SAVE_DEPT_OT_FAILED:String = "saveDeptOtFailed";
		
		public var list:ArrayCollection;
		public var orgeh:String;
		
		
		public function DeptOtProxy(data:Object=null)
		{
			super(NAME, data);
		}


		//병원목록
		public function load(d:Date, orgeh:String):void
		{
			
			var params:Object = new Object();
			params.I_DATE = DateUtil.toBasicString(d);
			params.I_PERNR = Variables.myPernr;
			params.I_ORGEH = orgeh;
			params.I_OPERATION = "DIS";
			
			loadData("Z_HR_0114", params, listResult, listFault);
		}
		
		public function save(d:Date, orgeh:String, otList:Array):void
		{
			var params:Object = new Object();
			params.I_DATE = DateUtil.toBasicString(d);
			params.I_PERNR = Variables.myPernr;
			params.I_ORGEH = orgeh;
			params.I_OPERATION = "MOD";
			
			var table:Object = new Object();
			table["T_OUT"] = otList;
			saveData("Z_HR_0114", params, table, saveResult, saveFault);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				list = parseList(event.result.T_OUT);
				facade.sendNotification(LOAD_DEPT_OT_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_DEPT_OT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_DEPT_OT_FAILED, this);
		}
		
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(SAVE_DEPT_OT_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_DEPT_OT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_DEPT_OT_FAILED, this);
		}
		
		
		
		
		
		
		
		
		private function parseList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:DeptOtVO = new DeptOtVO;
				vo.ORGEH = rec.ORGEH;
				vo.ORGTX = rec.ORGTX;
				vo.OTWOK = rec.OTWOK;
				vo.READ_ONLY = !(rec.READ_ONLY == "X");
				list.addItem(vo);
			}
			return list;
		}	
	}
}